(function($){

	/**
	 * 主函数
	 */
	$(function(){
		addblur();
		$("#strainerBrand").change(function(){ 
			changeStrainerByStrainerBrand($(this).val());
	    });
	   $("#oilBrandId").change(function(){ 
	   	   changeOilByOilBrand($(this).val());
	   });
	});
	
	/**
	 * 获取车型的方法
	 */
	
	$("#getCar").click(function(){
	    var id = $("#strainer").val();
	    var libre = $("#libre").val();
		var d = {"id":id,"libre":libre};
        var webRoot = $("#webRoot").val();
        var url = webRoot+"/cms/manage/goods/strainer/strainer/getCarByStrainerIdAndLibre";
        $.post(url,d,function(data){
            var node = "<tr id='cars'><td>车型:</td><td>";
            for(var i = 0;i<data.length;i++) {
               var dd = data[i];
               node+="<input class='car' type='checkbox' name='carIds' value='"+dd.id+"'/>"+dd.name; 
            }
            node+="<input type='checkbox' id='selectAllCar' />全选</td></tr>";
            $("#cars").remove();
            $("#beforCar").after(node);
            /*
             * 设置触发时间
             */
            $("#selectAllCar").on("click",function(){
               if($(this).attr("checked")) {
               	  $(".car").attr("checked","checked");
               }else {
               	  $(".car").removeAttr("checked");
               }
            });
        },"json");
        
        return false;
		
	});
	
	/**
	 * 根据城市和机油品牌获取代理商
	 */
	$("#getOilagent").click(function(){
	
		var cityId = $("#city").val();
	    var oilbrandId = $("#oilBrand").val();
		var d = {"cityId":cityId,"oilbrandId":oilbrandId};
        var webRoot = $("#webRoot").val();
        var url = webRoot+"/cms/manage/oilagent/getOilAgents";
        $.post(url,d,function(data){
            var node = "<tr id='oilagents'><td>代理商:</td><td><select name='oilagentId'>";
            for(var i = 0;i<data.length;i++) {
               var dd = data[i];
               node+="<option value='"+dd.id+"'>"+dd.linkName+"</option>"; 
            }
            node+="</select></td></tr>";
            $("#oilagents").remove();
            $("#beforOilagents").after(node);
        },"json");
        return false;
	});
	
	/**
	 * 根据机油品牌改变机油
	 * 
	 */
	function changeOilByOilBrand(id) {
		var d = {"id":id};
        var webRoot = $("#webRoot").val();
        var url = webRoot+"/cms/manage/goods/oil/oil/getOilByOilBrand";
        $.post(url,d,function(data){
            var node = "";
            for(var i = 0;i<data.length;i++) {
               var dd = data[i];
               node+="<span class='oilWrap'><input class='oil' type='checkbox' name='oilIds' value='"+dd.id+"' libre='"+dd.libre+"'/>"+dd.name+"</span>" 
            }
            $("#oil").html(node);
            $("#libre").val(0);
            $(".oil").on("click",setLibre);
        },"json");
	}
	
	/**
	 * 根据滤芯的品牌改变滤芯
	 */
	function changeStrainerByStrainerBrand(id) {
		var d = {"id":id};
        var webRoot = $("#webRoot").val();
        var url = webRoot+"/cms/manage/goods/strainer/strainer/getStrainerByStrainerBrand";
        $.post(url,d,function(data){
            var node = "";
            for(var i = 0;i<data.length;i++) {
               var dd = data[i];
               node+="<option  value='"+dd.id+"'>"+dd.name+"</option>"; 
            }
            $("#strainerId").html(node);
            
        },"json");
	}


	$(".oil").on("click",setLibre);
	
	/**
	 * 根据点击的事件获取机油的容量放到套餐的容量中
	 */
	function setLibre(){
		/**
		 * 选中
		 */
		if($(this).attr("checked")) {
            var libre = $("#libre").val();
            if(libre == "") {
            	libre = 0;
            }
            libre = parseInt(libre);
            var l = $(this).attr("libre");
            libre = libre +parseInt(l);
            $("#libre").val(libre);
            var node = "&nbsp;&nbsp;<input type='text' oldsize='1' libre='"+$(this).attr("libre")+"'  name='oilSizes' size='2' maxlenght='2' value='1' class='oilSizes'/>";
            
            $(this).parent().append(node);
            
            /*
             * 改变对话框时改变容量
             */
           addblur();

		}else {
			var libre = $("#libre").val();
			if(libre == "") {
            	libre = 0;
            }
            libre = parseInt(libre);
            
            var old = $(this).next();
            var l = parseInt(old.attr("libre"));
	        var oldSize =  parseInt(old.attr("oldSize"));
	        libre = libre - l*oldSize;
	        $("#libre").val(libre);
	        old.remove();
		}
	}
	
	
	/**
	 * 给输入框添加一个blur的时事件
	 */
	function addblur() {
		 $(".oilSizes").blur(function(){
            	var libre = $("#libre").val();
	            if(libre == "") {
	            	libre = 0;
	            }
	            libre = parseInt(libre);
	            var l = parseInt($(this).attr("libre"));
	            var size = parseInt($(this).val());
	            var oldSize =  parseInt($(this).attr("oldSize"));
	            libre = libre +parseInt(l)*(size-oldSize);
	            $("#libre").val(libre);
	            $(this).attr("oldSize",size);
            });
	}
	
	
	
	
})(jQuery);